﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OBiCMSFramework;

namespace ExampleWidgets
{
    public class SimpleWidget : OBiCMSFramework.WidgetBase
    {
        // Private Variables
        private Int32 _counter = 0;
        
        // Public Constructors
        public SimpleWidget(String id)
            : base(id)
        {
            // Set the refresh time between 10 and 60 seconds.
            MinRefreshTime = 10000;
            MaxRefreshTime = 60000;
        }

        // Public Methods
        public override List<String> DefaultSettings()
        {
            // This method will be implemented when we make a CMS Editor and will return a list
            // of settings which the CMS Editor will ask the user for, they will then be placed
            // in the Pages settings XML.
            throw new NotImplementedException();
        }

        // Protected Methods
        protected override void LoadWidget()
        {
            _counter += 1;
            DateChanged = DateTime.Now;
        }
        public override StringBuilder PostBack(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, System.Diagnostics.Stopwatch timer, Boolean useDiagnostics)
        {
            // You can put anything you need here :)
            context.SetSession("##Form##", Guid.NewGuid().ToString());
            return GenerateWidget(context, pageSettings, widgetSettings, timer, useDiagnostics);
        }
        public override StringBuilder GenerateWidget(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, System.Diagnostics.Stopwatch timer, Boolean useDiagnostics)
        {
            StringBuilder returnBody = new StringBuilder();

            if (useDiagnostics)
                returnBody.Append("<!-- Started Generating Widget at " + timer.ElapsedTicks + " -->");

            // Some simple text to output on the page.
            returnBody.Append("<span>Counter : " + _counter.ToString() + "</ span><br />");
            returnBody.Append("<span>Date updated : " + DateChanged.ToString() + "</ span><br />");
            returnBody.Append("<span>Submited? : " + context.GetSession("##Form##").ToString() + "</ span><br />");

            // Each widget gets given access to the pages PageSettings class, this enables the widgets to change
            // areas of the page that it does not have direct access to.
            //
            // See the documentation for more information about the PageSettings class.
            //
            pageSettings.AppendMetaKeywords("Keyword from widget");
            pageSettings.AppendTitle("Page title from widget");

            if (useDiagnostics)
                returnBody.Append("<!-- Finsihed Generating Widget at " + timer.ElapsedTicks + " -->");

            return returnBody;
        }

    }
}
